package com.eric.windows;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.widget.ImageView;

import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class ThreadTestActivity extends Activity  {

    public static final String IMG_URL = "http://img2.pconline.com.cn/pconline/0706/19/1038447_34.jpg";

    private ImageView imgThread;
    HttpURLConnection httpURLConnection = null;
    private ImageView img_handle;
    private ImageView img_async;
    //声明一个Handle
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case 21://处理请求结果
                    img_handle.setImageBitmap((Bitmap) msg.obj);
                    break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_thread_test);
        imgThread = (ImageView) findViewById(R.id.img_thread);
        img_handle = (ImageView) findViewById(R.id.img_handle);
        img_async = (ImageView) findViewById(R.id.img_async);

        new  Thread(new Runnable() {
            @Override
            public void run() {
                final Bitmap bitmap  = loadImage(IMG_URL);
              //处理方式一
              ThreadTestActivity.this.runOnUiThread(new Runnable() {
                  @Override
                  public void run() {
                      if (bitmap != null){
                          imgThread.setImageBitmap(bitmap);
                      }
                  }
              });
                //处理方式二
                //发送请求结果
                Message message =  handler.obtainMessage();
                message.what = 21;
                message.obj = bitmap;
                handler.sendMessage(message);
            }
        }).start();
        //处理方式三 不要和上面的同时使用
//        new DownloadImageTask().execute(IMG_URL);

    }


    /**
     * 下载图片
     * (此处输入方法执行任务.)
     * <h3>Version</h3> 1.0
     * <h3>CreateTime</h3> 2017/7/13,11:43
     * <h3>UpdateTime</h3> 2017/7/13,11:43
     * <h3>CreateAuthor</h3> Eric
     * <h3>UpdateAuthor</h3>
     * <h3>UpdateInfo</h3> (此处输入修改内容,若无修改可不写.)
     * @return
     */
    private Bitmap loadImage(String imgUrl){
        Bitmap bitmap = null;
        try {
            URL url = new URL(imgUrl);
            try {
                httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setDoInput(true);
                /*设置请求方式*/
                httpURLConnection.setRequestMethod("GET");
                httpURLConnection.connect();
                if (httpURLConnection.getResponseCode() == 200){
                    /*把字节数入流转化为BitMap位图*/
                    bitmap = BitmapFactory.decodeStream(httpURLConnection.getInputStream());
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();

        }
        return bitmap;
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (httpURLConnection != null){
            httpURLConnection.disconnect();
        }
    }

    //处理方法三
    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap>{

        //在这个方法中处理耗时任务
        @Override
        protected Bitmap doInBackground(String... params) {
            String imgUrl = params[0];
            if (TextUtils.isEmpty(imgUrl)){//如果imgUrl 为null 则解析默认图片
                return BitmapFactory.decodeResource(ThreadTestActivity.this.getResources(),R.mipmap.ic_launcher);
            }
            return loadImage(imgUrl);
        }
        //在这个方法中对请求结果进行处理
        @Override
        protected void onPostExecute(Bitmap bitmap) {
            super.onPostExecute(bitmap);
            if (bitmap != null){
                img_async.setImageBitmap(bitmap);
            }
        }
    }

}
